package product;

import java.awt.EventQueue;

import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.border.TitledBorder;
import javax.swing.border.BevelBorder;
import javax.swing.JTextField;
import javax.swing.UIManager;

import java.awt.Color;

import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JPanel;
import java.awt.Font;
import javax.swing.SwingConstants;
import java.awt.SystemColor;
import javax.swing.border.LineBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.JFrame;

public class ProductUpdate extends JInternalFrame {

	ProductMain4 pm;
	int ea = 0;
	int eaa = 0;

	private JTextField P_num;
	private JTextField P_code;
	private JTextField P_nm;
	private JTextField P_ea;
	private JTextField P_mid;
	private JButton btnUpdate;
	JTextField search;
	private JButton btnNewButton_1;
	private JLabel result;
	private JButton btnNewButton_2;
	private JPanel panel;
	private JLabel label_5;
	private JLabel label_6;
	private JLabel label_7;
	private JLabel label_8;
	private JLabel lblProductNo;
	private JPanel panel_2;
	private JLabel label;
	private JPanel panel_1;
	private JLabel lblProductInput;
	private JLabel lblProductUpdate;

	public ProductUpdate(ProductMain4 pm) {
		super("ProductUpdate", false, true, false, true);
		setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		getContentPane().setBackground(Color.WHITE);
		setTitle("생산 수정");
		setBounds(100, 100, 635, 495);
		getContentPane().setLayout(null);
		getContentPane().add(getResult());
		getContentPane().add(getPanel());
		getContentPane().add(getPanel_1_1());
		getContentPane().add(getLblProductUpdate());
		this.pm = pm;

	}

	public void setupdate() {
		String sql = "update product set ea=?, member_id=? where num=?";
		int n = Integer.parseInt(P_ea.getText());
		String name = P_mid.getText();
		int num = Integer.parseInt(P_num.getText());
		try {
			PreparedStatement pstmt = pm.conn.prepareStatement(sql);
			eaa = n;
			pstmt.setInt(1, n);
			pstmt.setString(2, name);
			pstmt.setInt(3, num);

			pstmt.executeUpdate();
			pstmt.close();

		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public void search() {
		String sql = "select p.num, p.code, p.ea, p.member_id, c.pro_nm "
				+ "from(select p.*, m.* from product p inner join memberdata m "
				+ "on p.member_id = m.memberid" + ")" + "p inner join code c "
				+ "on p.code = c.pro_code " + "where p.num=?";
		int n = Integer.parseInt(search.getText());

		try {
			PreparedStatement pstmt = pm.conn.prepareStatement(sql);
			pstmt.setInt(1, n);
			ResultSet rs = pstmt.executeQuery();

			if (rs.next()) {

				ea = Integer.parseInt(rs.getString(3));

				P_num.setText(rs.getString(1));
				P_code.setText(rs.getString(2));
				P_nm.setText(rs.getString(5));
				P_ea.setText(rs.getString(3));
				P_mid.setText(rs.getString(4));

				search.setText("");
				search.requestFocus();

			} else {
				JOptionPane.showMessageDialog(null, "찾는 생산번호가 없습니다.");
				search.setText("");
				search.requestFocus();
			}

		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	private JTextField getP_num() {
		if (P_num == null) {
			P_num = new JTextField();
			P_num.setBounds(234, 78, 180, 30);
			P_num.setEditable(false);
			P_num.setColumns(10);
		}
		return P_num;
	}

	private JTextField getP_code() {
		if (P_code == null) {
			P_code = new JTextField();
			P_code.setBounds(234, 123, 180, 30);
			P_code.setEditable(false);
			P_code.setColumns(10);
		}
		return P_code;
	}

	private JTextField getP_nm() {
		if (P_nm == null) {
			P_nm = new JTextField();
			P_nm.setBounds(234, 168, 180, 30);
			P_nm.setEditable(false);
			P_nm.setColumns(10);
		}
		return P_nm;
	}

	private JTextField getP_ea() {
		if (P_ea == null) {
			P_ea = new JTextField();
			P_ea.setBounds(234, 258, 180, 30);
			P_ea.setColumns(10);
		}
		return P_ea;
	}

	private JTextField getP_mid() {
		if (P_mid == null) {
			P_mid = new JTextField();
			P_mid.setBounds(234, 213, 180, 30);
			P_mid.setColumns(10);
		}
		return P_mid;
	}

	private JButton getBtnNewButton() {
		if (btnUpdate == null) {
			btnUpdate = new JButton("Save");
			btnUpdate.setEnabled(false);
			btnUpdate.setBorder(new LineBorder(Color.GRAY));
			btnUpdate.setBounds(434, 258, 100, 30);
			btnUpdate.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 15));
			btnUpdate.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					int i = JOptionPane.showConfirmDialog(null,
							"" + P_ea.getText() + "" + "데이터를 수정을 하시겠습니까?");
					if (i == JOptionPane.YES_OPTION) {
						setupdate();

						try {
							ProStock ps = new ProStock();
							ps.update(P_code.getText(), ea, eaa);
						} catch (Exception ee) {
						}

						P_code.setText("");
						P_ea.setText("");
						P_mid.setText("");
						P_nm.setText("");
						P_num.setText("");

						pm.tList.searchData();

						btnUpdate.setEnabled(false);

					}
				}
			});
		}
		return btnUpdate;
	}

	private JTextField getSearch() {
		if (search == null) {
			search = new JTextField();
			search.setBounds(123, 10, 267, 30);
			search.setColumns(10);
		}
		return search;
	}

	private JButton getBtnNewButton_1() {
		if (btnNewButton_1 == null) {
			btnNewButton_1 = new JButton("Search");
			btnNewButton_1.setBounds(406, 10, 100, 30);
			btnNewButton_1.setBorder(new LineBorder(Color.GRAY));
			btnNewButton_1.setFont(new Font("Microsoft YaHei UI", Font.PLAIN,
					15));
			btnNewButton_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					
					if(search.getText().equals("")){
						JOptionPane.showMessageDialog(null, "입력해주세요.");
						return;
					}
					String sql = "select p.num, p.code, p.ea, p.member_id, c.pro_nm "
							+ "from(select p.*, m.* from product p inner join memberdata m "
							+ "on p.member_id = m.memberid"
							+ ")"
							+ "p inner join code c "
							+ "on p.code = c.pro_code " + "where p.num=?";
					int n = Integer.parseInt(search.getText());

					try {
						PreparedStatement pstmt = pm.conn.prepareStatement(sql);
						pstmt.setInt(1, n);
						ResultSet rs = pstmt.executeQuery();

						if (rs.next()) {

							ea = Integer.parseInt(rs.getString(3));

							P_num.setText(rs.getString(1));
							P_code.setText(rs.getString(2));
							P_nm.setText(rs.getString(5));
							P_ea.setText(rs.getString(3));
							P_mid.setText(rs.getString(4));

							search.setText("");
							search.requestFocus();

						} else {
							JOptionPane.showMessageDialog(null,
									"찾는 생산번호가 없습니다.");
							search.setText("");
							search.requestFocus();
						}

					} catch (Exception ex) {
						ex.printStackTrace();
					}

				}
			});
		}
		return btnNewButton_1;
	}

	private JLabel getResult() {
		if (result == null) {
			result = new JLabel("");
			result.setBounds(137, 300, 126, 20);
		}
		return result;
	}

	private JButton getBtnNewButton_2() {
		if (btnNewButton_2 == null) {
			btnNewButton_2 = new JButton("Check");
			btnNewButton_2.setBorder(new LineBorder(Color.GRAY));
			btnNewButton_2.setFont(new Font("Microsoft JhengHei UI",
					Font.PLAIN, 15));
			btnNewButton_2.setBounds(433, 213, 100, 30);
			btnNewButton_2.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					try {
						boolean flag = true;
						Statement st = pm.conn.createStatement();

						String sql = "select irum from memberdata where memberid='"
								+ P_mid.getText() + "'";
						ResultSet rs = st.executeQuery(sql);

						while (rs.next()) {
							String name = rs.getString(1);
							JOptionPane.showMessageDialog(null, name
									+ "님 확인되셨습니다.");

							flag = false;
							btnUpdate.setEnabled(true);
						}// while---------

						if (flag) {
							JOptionPane
									.showMessageDialog(null, "해당 아이디가 없습니다.");
							P_mid.setText("");
							P_mid.requestFocus();
							btnUpdate.setEnabled(false);
						}
						rs.close();
						st.close();

					} catch (Exception ee) {
					}
				}
			});
		}
		return btnNewButton_2;
	}

	private JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.setBorder(new LineBorder(Color.GRAY, 2));
			panel.setBackground(SystemColor.inactiveCaptionBorder);
			panel.setBounds(24, 125, 570, 317);
			panel.setLayout(null);
			panel.add(getLabel_5());
			panel.add(getLabel_6());
			panel.add(getLabel_7());
			panel.add(getLabel_8());
			panel.add(getLblProductNo());
			panel.add(getP_mid());
			panel.add(getP_ea());
			panel.add(getP_nm());
			panel.add(getP_code());
			panel.add(getP_num());
			panel.add(getBtnNewButton_2());
			panel.add(getBtnNewButton());
			panel.add(getPanel_2());
		}
		return panel;
	}

	private JLabel getLabel_5() {
		if (label_5 == null) {
			label_5 = new JLabel("Product Code");
			label_5.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 15));
			label_5.setBounds(42, 123, 170, 30);
		}
		return label_5;
	}

	private JLabel getLabel_6() {
		if (label_6 == null) {
			label_6 = new JLabel("Product Name");
			label_6.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 15));
			label_6.setBounds(42, 168, 170, 30);
		}
		return label_6;
	}

	private JLabel getLabel_7() {
		if (label_7 == null) {
			label_7 = new JLabel("Employee Name");
			label_7.setHorizontalAlignment(SwingConstants.LEFT);
			label_7.setFont(new Font("Microsoft JhengHei UI", Font.PLAIN, 15));
			label_7.setBounds(42, 213, 170, 30);
		}
		return label_7;
	}

	private JLabel getLabel_8() {
		if (label_8 == null) {
			label_8 = new JLabel("Quantity");
			label_8.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 15));
			label_8.setBounds(42, 258, 170, 30);
		}
		return label_8;
	}

	private JLabel getLblProductNo() {
		if (lblProductNo == null) {
			lblProductNo = new JLabel("Product No");
			lblProductNo
					.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 15));
			lblProductNo.setBounds(42, 78, 170, 30);
		}
		return lblProductNo;
	}

	private JPanel getPanel_2() {
		if (panel_2 == null) {
			panel_2 = new JPanel();
			panel_2.setBounds(24, 16, 520, 50);
			panel_2.setBackground(Color.WHITE);
			panel_2.setBorder(new EtchedBorder(EtchedBorder.LOWERED,
					Color.GRAY, null));
			panel_2.setLayout(null);
			panel_2.add(getSearch());
			panel_2.add(getBtnNewButton_1());
			panel_2.add(getLabel());
		}
		return panel_2;
	}

	private JLabel getLabel() {
		if (label == null) {
			label = new JLabel("Product No");
			label.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 15));
			label.setBounds(14, 10, 170, 30);
		}
		return label;
	}

	private JPanel getPanel_1_1() {
		if (panel_1 == null) {
			panel_1 = new JPanel();
			panel_1.setLayout(null);
			panel_1.setBorder(new LineBorder(Color.GRAY, 2));
			panel_1.setBounds(-21, -21, 702, 104);
			panel_1.add(getLblProductInput());
		}
		return panel_1;
	}

	private JLabel getLblProductInput() {
		if (lblProductInput == null) {
			lblProductInput = new JLabel("Product Update");
			lblProductInput.setFont(new Font("Microsoft YaHei UI", Font.PLAIN,
					40));
			lblProductInput.setBounds(178, 31, 325, 61);
		}
		return lblProductInput;
	}

	private JLabel getLblProductUpdate() {
		if (lblProductUpdate == null) {
			lblProductUpdate = new JLabel("Product Information");
			lblProductUpdate.setForeground(Color.DARK_GRAY);
			lblProductUpdate.setFont(new Font("Microsoft YaHei UI", Font.PLAIN,
					20));
			lblProductUpdate.setBounds(24, 94, 224, 30);
		}
		return lblProductUpdate;
	}
}
